package com.example.myson;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    TextView time;
    ListView listView;
    List<User> list=new ArrayList<User>();
    User user;
    EditText editText;
    Button button;
    int iiiii=0;

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Toast.makeText(MainActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
            time.setText(Urlhttp.gettime());
            Myadater myadater=new Myadater();
            listView.setAdapter(myadater);
            editText.setText("");
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        Urlhttp.transparentStatusBar(this);
        setContentView(R.layout.activity_main);
        time= (TextView) findViewById(R.id.id_time);


        listView= (ListView) findViewById(R.id.id_listview);
        registerForContextMenu(listView);
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                iiiii=position;
                return false;
            }
        });
        button= (Button) findViewById(R.id.sendbtn);
        editText= (EditText) findViewById(R.id.id_edtext);
        time.setText(Urlhttp.gettime());
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String path="http://www.tuling123.com/openapi/api?key=47b3ff86d8d746698d2173bedbf627fd&info="+editText.getText();
                Urlhttp.getString(path,new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);

                        try {
                            JSONObject result=new JSONObject(msg.obj.toString());
                            String cade=result.getString("code");
                            String text=result.getString("text");
                            user=new User();
                            user.setSend(editText.getText().toString());
                            user.setMassage(text);
                            Log.d("QQQQQQQQQ",text);
                            Myadater myadater=new Myadater();
                            listView.setAdapter(myadater);
                            editText.setText("");
                            list.add(user);
                            if (list.size()==8){
                                list.clear();
                                Toast.makeText(MainActivity.this, "第八条自动清空聊天记录", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                });
            }
        });
    }


    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.menu,menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.delete:
                if (iiiii>=1){
                    list.remove(iiiii);
                    Toast.makeText(this, "差不多删除成功了", Toast.LENGTH_SHORT).show();
                    listView.setAdapter(new Myadater());
                }else{
                    Toast.makeText(this, "注意，第一条不能删", Toast.LENGTH_SHORT).show();
                }
                break;
        }
        return false;
    }
    public class Myadater extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            User user=list.get(position);
            View view=getLayoutInflater().inflate(R.layout.listitem,null);
            TextView textView= (TextView) view.findViewById(R.id.id_text);
            TextView textView1= (TextView) view.findViewById(R.id.id_textme);
            textView1.setText(user.getSend());
            textView.setText(user.getMassage());
            return view;
        }
    }

}
